<?php

/*结构与小程序结构相似, 在build文件夹下构建页面，在build/app.json里注册页面，页面由[?.html/?.js/?.css]构成*/
/*编译完成后，可以删除build文件夹和index.php (仅index.html + resoures 即可访问)*/
/*可以同时和jquery等其他js框架并同时使用*/
/*可以dcloud、 apicloud 快速构建app*/
/*可以和nw.js快速构建桌面运用*/

error_reporting(0);
session_start();
$scripts = [];
$_SESSION['scripts'] = $scripts;


function ckDir($dir) 
{
	return is_dir($dir) or (ckDir(dirname($dir)) and mkdir($dir, 0777));
}


function appDataBase($root = 'build')/*相对目录默认从build文件夹开始*/
{

	file_exists("$root/app.html") || file_put_contents("$root/app.html", "<router-view></router-view>");
	file_exists("$root/app.js") || file_put_contents("$root/app.js", "App({\n\n})");
	file_exists("$root/app.json") || file_put_contents("$root/app.json", "{\n\"pages\": []\n}");

	/*读取html & css*/
	$app = [
		'html' 	=> str_replace(["\r\n","\t","\n","\r"], '', file_get_contents( "$root/app.html")) ,
		'js'	=> file_get_contents("$root/app.js"),
		'json'	=> json_decode(file_get_contents("$root/app.json"), true)
	];
	//$pages = $app['json']['pages'];
	$pages = $app['json']['components'] ? array_merge($app['json']['pages'],$app['json']['components']) :  $app['json']['pages'];

	$tempJS = '';
	foreach ($pages as $key => $value) {
		$dir = $root . '/' . str_replace('.', '/', $value);

		ckDir(pathinfo($dir)['dirname']);
		file_exists("$dir.html") || file_put_contents("$dir.html","<div>\n\t" . time() . " $value\n</div>");
		file_exists("$dir.css") || file_put_contents("$dir.css",'');
		file_exists("$dir.js") || file_put_contents("$dir.js","Page({\n\n})\n");

		$html = str_replace(["  ","   "], ' ', file_get_contents("$dir.html") );
		$css = file_get_contents("$dir.css");
		$js = file_get_contents("$dir.js");
		$app['pages'][$value] = [
			'html'	=> str_replace(["  ","\r\n","\t","\n","\r"], '', $html),
			'css'	=> $css
		];	

		$tempJS .= $js ? "currentPage = \"$value\";\n$js;\n" : "currentPage = \"$value\";\nPage({})\n";
	}
	$tempJS = formatJS($tempJS, $root);
	$scripts = $_SESSION['scripts'];

	$app['tempJS'] = $tempJS;
	$app['scripts'] = $scripts;
	return json_encode($app);
}

function formatJS($js, $root)
{
	preg_match_all('/requireJs\(\'(.*)\'\)/', $js, $matches, PREG_OFFSET_CAPTURE);
	if($matches[0]) {
		/*存在，即替换*/
		foreach ($matches[1] as $key => $value) {
			$requirePath = $value[0];
			$jsText = file_get_contents('./' . $root. '/' . $value[0]);
			
			/*保存为Key*/
			$jsText = formatJS($jsText, $root);
			$scripts = $_SESSION['scripts'];
			$scripts[$requirePath] = $jsText;
			$_SESSION['scripts'] = $scripts;
			
			$js =  str_replace('requireJs',"jsDecode",$js);
		}
	} 	
	
	preg_match_all('/require\(\'(.*)\'\)/', $js, $matches, PREG_OFFSET_CAPTURE);
	if($matches[0]) {
		/*存在，即替换*/
		foreach ($matches[1] as $key => $value) {
			$requirePath = $value[0];
			$jsText = file_get_contents('./' . $root. '/' . $value[0]);
			
			/*保存为Key*/
			$jsText = formatJS($jsText, $root);
			$scripts = $_SESSION['scripts'];
			$scripts[$requirePath] = $jsText;
			$_SESSION['scripts'] = $scripts;
			
			$js =  str_replace('require',"jsDecode",$js);
		}
	} 	

	/*兼容apicloud 的 api.require方法*/
	preg_match_all('/\.jsDecode\(\'(.*)\'\)/', $js, $matches, PREG_OFFSET_CAPTURE);
	if($matches[0]) {
		/*存在，即替换*/
		foreach ($matches[1] as $key => $value) {
			$requirePath = $value[0];
			$jsText = file_get_contents('./' . $root. '/' . $value[0]);
			
			/*保存为Key*/
			$jsText = formatJS($jsText, $root);
			$scripts = $_SESSION['scripts'];
			$scripts[$requirePath] = $jsText;
			$_SESSION['scripts'] = $scripts;
			
			$js =  str_replace(".jsDecode",".require",$js);
		}
	} 

	
	return $js;
}

/*生成appBaseData文件*/
ckDir("./resourse/data");
file_put_contents('resourse/data/data.js', "var Dir = 'build';\nvar baseData = " . appDataBase());
exit(file_get_contents('./index.html'));

?>


